home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8279 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: tudelft.nl!news
  2. From: h.huisman@wbmt.tudelft.nl [Henk Huisman]
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: 16 Feb 1996 13:16:19 GMT
  6. Organization: Delft University of Technology, The Netherlands
  7. Message-ID: <4g2033$cvr@mo6.rc.tudelft.nl>
  8. References: <4fr8be$ass@news.iconn.net>
  9. Reply-To: h.huisman@wbmt.tudelft.nl
  10. NNTP-Posting-Host: dutw195.tudelft.nl
  11. X-Newsreader: IBM NewsReader/2 v1.02
  12.  
  13. In <4fr8be$ass@news.iconn.net>, thecrow@iconn.net (The Crow) writes:
  14. >Here is what I am trying to do, I want to find the last NON-ZERO digit of a 
  15. >given factorial.  For instance,
  16. >
  17. >5! = 120
  18. >
  19. >so the last non-zero digit is 2.  I want to be able to do this up to 1000.  
  20. >Problem is, no matter how huge of a data-type you use, there isn't any way for 
  21. >the computer to handle 1000!, it is however possible to find the last non-zero 
  22. >digit somehow.  One thing I have tried is as I went through mulitplying the 
  23. >series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the 
  24. >trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i 
  25. >am not really sure why.  If anyone has a clue how I can do this let me know.
  26. >
  27. >-- 
  28. >The Crow - thecrow@iconn.net
  29. >"It can't rain all the time"
  30. >-Kryptology
  31. >
  32.  
  33. You will get a zero at the end for every factor of 5 in the number:
  34. 2's are available in abundance. 
  35. So: nzeroes:=0;
  36.     for i:=1 to n
  37.     do begin
  38.          nfives:={factor i, not too hard, and get the exponent of 5.}
  39.          nzeroes:=nzeroes+nfives;
  40.        end;
  41.  
  42.  
  43. Henk.
  44.  
  45. -----This is an airconditioned room, do not open Windows.-----
  46. Henk Huisman                         h.huisman@wbmt.tudelft.nl
  47. Systems and Control Group            Mekelweg 2
  48. Delft University of Technology       NL-2628 CD Delft
  49. --------------------------------------------------------------
  50.  
  51.